package com.jarvis.app.car;


import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Looper;
import android.os.Message;
import android.text.TextUtils;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

import androidx.activity.EdgeToEdge;
import androidx.activity.result.ActivityResultLauncher;
import androidx.activity.result.contract.ActivityResultContract;
import androidx.activity.result.contract.ActivityResultContracts;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;
import androidx.core.graphics.Insets;
import androidx.core.view.ViewCompat;
import androidx.core.view.WindowInsetsCompat;

import com.jarvis.app.car.common.MessageResult;
import com.jarvis.app.car.constant.AppConstant;
import com.jarvis.app.car.model.User;
import com.jarvis.app.car.util.ServiceManager;
import com.jarvis.app.car.util.ThreadPoolManager;

import java.util.Objects;

public class LoginActivity extends AppCompatActivity {
    private TextView tv_register;

    private EditText et_userName;

    private EditText et_password;

    private Button bt_login;

    private final Handler handler = new Handler(Looper.getMainLooper()) {

        @Override
        public void handleMessage(@NonNull Message message) {
            MessageResult<User> messageResult = (MessageResult<User>) message.obj;
            if (!messageResult.isJudgeSuccess()) {
                Toast.makeText(LoginActivity.this, messageResult.getMsg(), Toast.LENGTH_SHORT).show();

            } else {
                startActivity(new Intent(LoginActivity.this, IndexActivity.class));
                User user = messageResult.getResult();
                AppConstant.ACCOUNT_ID = user.getId();
                AppConstant.ACCOUNT_NAME = user.getName();
                AppConstant.ACCOUNT_PHONE = user.getPhone();
                AppConstant.ACCOUNT_SUPER = user.getBolSuper();
                AppConstant.ACCOUNT_IMG = user.getImg();
                finish();
            }
        }
    };


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        EdgeToEdge.enable(this);
        setContentView(R.layout.activity_login);
        ViewCompat.setOnApplyWindowInsetsListener(findViewById(R.id.main), (v, insets) -> {
            Insets systemBars = insets.getInsets(WindowInsetsCompat.Type.systemBars());
            v.setPadding(systemBars.left, systemBars.top, systemBars.right, systemBars.bottom);
            return insets;
        });

        tv_register = findViewById(R.id.tv_register);
        et_userName = findViewById(R.id.et_userName);
        et_password = findViewById(R.id.et_password);
        bt_login = findViewById(R.id.bt_login);


        tv_register.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(LoginActivity.this, RegisterActivity.class);
                startActivity(intent);
            }
        });

        bt_login.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String userName = et_userName.getText().toString().trim();
                String password = et_password.getText().toString().trim();
                if (TextUtils.isEmpty(userName) || TextUtils.isEmpty(password)) {
                    Toast.makeText(LoginActivity.this, "用户名/密码不能为空", Toast.LENGTH_SHORT).show();
                    return;
                }

                ThreadPoolManager.threadPoolExecutor.execute(new Runnable() {
                    @Override
                    public void run() {
                        MessageResult<User> one = ServiceManager.userService.getOne(userName, password);
                        Message msg = Message.obtain();
                        msg.obj = one;
                        handler.sendMessage(msg);
                    }
                });


            }
        });

    }
}